<template>
  <el-card>
    <el-breadcrumb separator="/" style="margin-bottom: 20px">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-form :inline="true" :model="goodsListQuery" class="demo-form-inline">
      <el-form-item label="商品名称">
        <el-input
          v-model="goodsListQuery.proname"
          placeholder="请输入商品名称进行搜索"
          clearable
          size="large"
        />
      </el-form-item>
      <el-form-item label="商品分类">
        <el-select
          v-model="goodsListQuery.category"
          placeholder="请选择商品分类进行搜索"
          size="large"
        >
          <el-option
            v-for="item in cateGories"
            :key="item"
            :label="item"
            :value="item"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="商品品牌">
        <el-input
          v-model="goodsListQuery.brand"
          placeholder="请输入商品品牌进行搜索"
          clearable
          size="large"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="clear">清空</el-button>
        <el-button type="primary" @click="getGoodsListData">搜索</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <el-card style="margin-top: 20px">
    <el-table :data="goodsList" style="width: 100%; text-align: center">
      <el-table-column type="index" label="序号" width="80" />
      <el-table-column prop="img1" label="轮播图图片" width="120">
        <template #default="{ row }">
          <el-image
            style="width: 100px; height: 100px"
            :src="row.img1"
            :preview-src-list="[row.img1, row.img2, row.img3, row.img4]"
            preview-teleported
          />
        </template>
      </el-table-column>
      <el-table-column prop="proname" label="名称" width="200" />
      <el-table-column label="品牌" prop="brand" />
      <el-table-column
        label="分类"
        prop="category"
        :filter-method="filterHandler"
        :filters="cateGoryFilter"
      />
      <el-table-column label="原价" prop="originprice" sortable />
      <el-table-column label="折扣" prop="discount" sortable />
      <el-table-column label="销量" prop="sales" sortable />
      <el-table-column label="是否售卖">
        <template #default="{ row }">
          <el-switch
            v-model="row.issale"
            :active-value="1"
            :inactive-value="0"
            @change="changeStatus(row.proid, 'issale', $event)"
          />
        </template>
      </el-table-column>
      <el-table-column label="是否秒杀">
        <template #default="{ row }">
          <el-switch
            v-model="row.isseckill"
            :active-value="1"
            :inactive-value="0"
            @change="changeStatus(row.proid, 'isseckill', $event)"
          />
        </template>
      </el-table-column>
      <el-table-column label="是否推荐">
        <template #default="{ row }">
          <el-switch
            v-model="row.isrecommend"
            :active-value="1"
            :inactive-value="0"
            @change="changeStatus(row.proid, 'isrecommend', $event)"
          />
        </template>
      </el-table-column>
      <el-table-column label="操作" width="100">
        <template #default="{ row }">
          <el-button type="primary" @click="detail(row)">查看详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:current-page="goodsListQuery.page"
      v-model:page-size="goodsListQuery.pageSize"
      :page-sizes="[10, 20, 50, 100]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="getGoodsListData"
      @current-change="getGoodsListData"
      class="paginationn"
    />
  </el-card>
  <el-dialog v-model="dialogVisible" fullscreen center>
    <template #header> 商品详情 </template>
    <template #default>
      <div style="width: 1000px; margin: 0 auto">
        <el-carousel height="600px">
          <el-carousel-item v-for="item in swiper" :key="item">
            <img :src="item" style="width: 100%" />
          </el-carousel-item>
        </el-carousel>
        <h3>{{ goods.proname }}</h3>
        <h4>品牌：{{ goods.brand }}</h4>
        <h4>分类：{{ goods.category }}</h4>
        <h4>
          价格：<span style="color: red"
            >￥{{ priceFormat.format(goods.originprice) }}</span
          >
        </h4>
        <h4>销量：{{ goods.sales }}</h4>
        <h4>是否售卖：{{ goods.issale === 1 ? '是' : '否' }}</h4>
        <h4>是否秒杀：{{ goods.isseckill === 1 ? '是' : '否' }}</h4>
        <h4>是否推荐：{{ goods.isrecommend === 1 ? '是' : '否' }}</h4>
        <h4 v-html="goods.desc"></h4>
        <h4>图文详情：</h4>
        <div><el-image :src="goods.img1"></el-image></div>
        <div><el-image :src="goods.img2"></el-image></div>
        <div><el-image :src="goods.img3"></el-image></div>
        <div><el-image :src="goods.img4"></el-image></div>
      </div>
    </template>

    <template #footer>
      <el-button @click="dialogVisible = false">关闭</el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { computed, ref } from 'vue'
import type { GoodsQuery, GoodsData } from '@/types/goods'
import { getCateGory, getGoodsList, updataStatus } from '@/api/goods'
import { usePriceFormat } from '@/hooks/formatPrice'
import { ElMessage } from 'element-plus'

// 查询商品数据
const goodsListQuery = ref({
  brand: '',
  category: '',
  page: 1,
  pageSize: 10,
  proname: ''
} as GoodsQuery)

// 获取分类信息
const cateGories = ref([] as string[])
const getCatGoryData = async () => {
  const res = await getCateGory()
  cateGories.value = res.data.data
}
getCatGoryData()

// 商品列表数据信息
const goodsList = ref([] as GoodsData[])
const total = ref(0)

// 查询商品列表信息
const getGoodsListData = async () => {
  console.log(goodsListQuery.value)

  const res = await getGoodsList(goodsListQuery.value)
  if (res.data.code === 200) {
    goodsList.value = res.data.data.items
    total.value = res.data.data.total
    console.log(res)
  }
}
getGoodsListData()

// 清空输入框
const clear = () => {
  goodsListQuery.value = {
    brand: '',
    category: '',
    page: 1,
    pageSize: 10,
    proname: ''
  }
  getGoodsListData()
}

// 改变三个状态框
const changeStatus = async (
  proid: string,
  type: 'issale' | 'isseckill' | 'isrecommend',
  state: 0 | 1
) => {
  const res = await updataStatus({ proid, type, state })

  ElMessage.success(res.data.message)
}

// 分类筛选
const cateGoryFilter = computed(() => {
  const beforeFilter = goodsList.value.map(item => item.category)
  return Array.from(new Set(beforeFilter)).map(item => ({
    text: item,
    value: item
  }))
})
const filterHandler = (value: string, row: GoodsData) => {
  return row.category === value
}

// 查看商品详情
const priceFormat = usePriceFormat()
const swiper = ref([] as string[])
const goods = ref({} as GoodsData)
const dialogVisible = ref(false)
const detail = (row: GoodsData) => {
  swiper.value = row.banners[0].split(',')
  goods.value = row
  dialogVisible.value = true
  goods.value.desc = `<p><img src="http://huangjiangjun.top:3001/admin/file/banners/a9f87a46-26ae-40d8-972d-44f1c7435c26" alt="" data-href="" style=""/></p>`
}
</script>

<style scoped>
.paginationn {
  margin-left: 316px;
  margin-top: 20px;
}
</style>
